<?php 
defined('ACC')||exit('ACC Denied');
class Model{
	protected $table=null;//是model所控制的表
	protected $primarykey='';
	protected $db=null;//是引入的mysql对象；
	protected $file=array();
	protected $autofill=array();
	protected $_valid=array();//自动验证
	protected $error=array();
	public function __construct(){
		$this->db=Mysql::getIns();
	}
	/**
	*普通增删改查功能实现
	*/
	public function table($table){
		return $this->table=$table;
	}
	/**
	*普通增删改查功能实现
	*@param arr //参数
	*@return int 收影响的行数 //返回
	*/
	public function add($data){
		return $this->db->autoExecute($this->table,$data,'insert');
	}
	//删
	public function delete($id){
		$sql='delete from '.$this->table.' where '.$this->primarykey.'='.$id;
		return $this->db->exec($sql);           
	}
	//改
	public function update($data,$id){
		return $this->db->autoExecute($this->table,$data,'update',' where '.$this->primarykey.'='.$id);
	}
	//查询所有行
	public function select(){
		$sql='select * from '.$this->table;
		return $this->db->getAll($sql);
	}
	//查询一行
	public function edit($id){
	$sql='select * from '.$this->table.' where '.$this->primarykey.'='.$id;

	$row=$this->db->getAll($sql);
	return $row;
	}
	/**
	*自动过滤
	*@param arr //参数
	*@return arr //返回
	*/
	function _facade($arr=array()){
		$data=array();
		foreach ($arr as $k => $v) {
			if (in_array($k,$this->file)) {
				$data[$k]=$v;
			}
		}
		return $data;
	
	} 
	/**
	*自动添加
	*@param arr //参数
	*@return arr //返回
	*/
	function _auto($arr=array()){ 
		foreach ($this->autofill as $k => $v) {
			if (!array_key_exists($v[0], $arr)) {
				switch ($v[1]) {
					case 'default':
						$arr[$v[0]]=$v[2];
						break;
					case 'func':
						$arr[$v[0]]=$v[2]();
						break;
				}
			}
		}
		return $arr;
		}
	/**
	*自动验证
	*@param arr //参数
	*@return arr //返回
	*/
	public function _validate($data){
		if (empty($this->_valid)) {
			return ture;
		}
		$this->error=array();
		foreach ($this->_valid as $k => $v) {
			switch ($v[1]) {
			case '1':
				if (!isset($data[$v[0]])||empty($data[$v[0]])) {
					$this->error=$v[2];
					return false;
				}
				break;
			case '0':
				if (isset($data[$v[0]])) {
					if (!$this->check($data[$v[0]],$v[3],$v[4])) {
						$this->error=$v[2];
						return false;
					}
				}
				
				break;
			case '2':
				if (isset($data[$v[0]]) && !empty($data[$v[0]])) {
					if (!$this->check($data[$v[0]],$v[3],$v[4])) {
						$this->error=$v[2];
						return false;
					}
				}
				
			}		
		}
		return true;
	}
	public function getErr(){
		return $this->error;
	}
	/**
	 * @param $value
	 * @param string $rule
	 * @param string $parm
	 * @return bool
	 */
	protected function check($value,$rule='',$parm=''){
		switch ($rule) {
			case 'require':
				return !empty($value);
				break;
			case 'number':
				return is_numeric($value);
			case 'email':
				filter_var($value,FILTER_VAL);
				break;
			case 'in':
				$tmp=explode(',',$value);
				return in_array($value,$tmp);
				break;
			case 'between':
				list($min,$max)=explode(',',$parm);
				return $value >= $min && $value <= $max;
				break;
			case 'length':
				list($min,$max)=explode(',',$parm);
				// echo $min.'<br>'.$max.'<br>';
				// echo strlen($value);
				return strlen($value) >= $min &&strlen($value) <= $max;
				break;
			default:
				return false;
				break;
		}
	}
}

	
